/****************************************************************************
**
** EntryText.cpp
**
** Copyright (C) August 2014 Hotride
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
*****************************************************************************
*/
//---------------------------------------------------------------------------
#include "stdafx.h"
//---------------------------------------------------------------------------
TEntryText::TEntryText()
: m_MaxLength(100), m_Position(0)
{
	memset(m_CData, 0, sizeof(m_CData));
	m_Text = L"";
}
//---------------------------------------------------------------------------
void TEntryText::Insert(wchar_t ch)
{
	if (m_Position < 0) m_Position = 0;
	if (m_Position > m_Text.length()) m_Position = m_Text.length();
	if (m_Text.length() >= m_MaxLength) return;
	m_Text.insert(m_Text.begin() + m_Position, ch);
	m_Position++;
}
//---------------------------------------------------------------------------
void TEntryText::Remove(bool left)
{
	if (left)
	{
		if (m_Position < 1) return;
		m_Position--;
	}
	else
	{
		if (m_Position >= m_Text.length()) return;
	}
	m_Text.erase(m_Text.begin() + m_Position);
}
//---------------------------------------------------------------------------
void TEntryText::Clear()
{
	m_Text = L"";
	memset(m_CData, 0, sizeof(m_CData));
	m_Position = 0;
}
//---------------------------------------------------------------------------
void TEntryText::SetMaxLength(int max)
{
	if (max < 1) max = 1;
	if (max > 100) max = 100;
	m_MaxLength = max;
}
//---------------------------------------------------------------------------
void TEntryText::AddPos(int val)
{
	m_Position += val;
	if (m_Position < 0) m_Position = 0;
	if (m_Position > m_Text.length()) m_Position = m_Text.length();
}
//---------------------------------------------------------------------------
void TEntryText::SetPos(int val)
{
	m_Position = val;
	if (m_Position < 0) m_Position = 0;
	if (m_Position > m_Text.length()) m_Position = m_Text.length();
}
//---------------------------------------------------------------------------
void TEntryText::SetText(const string &text)
{
	Clear();
	wchar_t str[100] = {0};
	MultiByteToWideChar(CP_ACP, 0, text.c_str(), text.length(), str, 100);
	m_Text = str;
	m_Position = m_Text.length();
}
//---------------------------------------------------------------------------
void TEntryText::SetText(const wstring &text)
{
	Clear();
	m_Text = text;
	m_Position = m_Text.length();
}
//---------------------------------------------------------------------------
void TEntryText::DrawA(BYTE font, WORD color, float X, float Y)
{
	if (Length()) UO->m_Font->DrawA(font, c_str(), color, X, Y);

	if (this == g_EntryPointer)
	{
		int TextLen = UO->m_Font->GetWidthA(font, m_CData, Pos());
		UO->m_Font->DrawA(font, "_", color, X + TextLen, Y);
	}
}
//---------------------------------------------------------------------------
void TEntryText::DrawW(BYTE font, WORD color, WORD flags, float X, float Y)
{
	if (Length()) UO->m_Font->DrawW(font, Data(), color, X, Y, 30, 0, TS_LEFT, flags);

	if (this == g_EntryPointer)
	{
		int TextLen = UO->m_Font->GetWidthW(font, Data(), Pos());
		UO->m_Font->DrawW(font, L"_", color, X + TextLen, Y, 30, 0, TS_LEFT, flags);
	}
}
//---------------------------------------------------------------------------